package com.fc.h5dispaly.h5display;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.os.Bundle;
import android.os.Process;
import android.support.multidex.MultiDex;
import android.text.TextUtils;
import android.util.Log;

import com.fc.h5dispaly.h5display.hotfix.FixDexUtils;
import com.fc.h5dispaly.h5display.util.BlockDetectByPrinter;
import com.fc.h5dispaly.h5display.util.HookUtil;
import com.github.moduth.blockcanary.BlockCanary;
import com.github.moduth.blockcanary.BlockCanaryContext;

import java.util.List;

/**
 * Created by fc on 2018/8/6.
 */

public class H5DisplayApplication extends Application implements Application.ActivityLifecycleCallbacks {
    private int appCount = 0;

    @Override
    public void onCreate() {
        super.onCreate();
        BlockCanary.install(this,new BlockCanaryContext()).start();

    }

    @Override
    public void onActivityCreated(Activity activity, Bundle bundle) {

    }

    @Override
    public void onActivityStarted(Activity activity) {
        appCount++;
    }

    @Override
    public void onActivityResumed(Activity activity) {

    }

    @Override
    public void onActivityPaused(Activity activity) {

    }

    @Override
    public void onActivityStopped(Activity activity) {
        appCount--;
    }

    @Override
    public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {

    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(base);
        String processName = getProcessName(this, Process.myPid());
        boolean isMainProcess = !TextUtils.isEmpty(processName) && processName.equals(getPackageName());
        Log.i("H5DisplayApplication", "isMainProcess: " + isMainProcess + "  " + processName + "   pid:" + Process.myPid());

        boolean hasExit = isServiceExisted(this," com.coocaa.libnetspeedup.database.service.NetSpeedDataBaseService");

        FixDexUtils.loadFixedDex(base);
        HookUtil.hookInstrumentation();
        BlockDetectByPrinter.start();

    }

    private String getProcessName(Context context, int pid) {
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> runningAppProcessInfoList = null;
        if (am != null) {
            runningAppProcessInfoList = am.getRunningAppProcesses();
        }
        if (runningAppProcessInfoList == null)
            return null;
        for (ActivityManager.RunningAppProcessInfo info : runningAppProcessInfoList) {
            if (info.pid == pid)
                return info.processName;
        }

        return null;
    }

    private boolean isServiceExisted(Context context, String className) {
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        if (am != null) {
            List<ActivityManager.RunningServiceInfo> serviceInfos = am.getRunningServices(Integer.MAX_VALUE);
            if (serviceInfos == null || serviceInfos.size() <= 0)
                return false;
            for (ActivityManager.RunningServiceInfo info : serviceInfos) {
                if (info.service.getClassName().equals(className))
                    return true;
            }
        }
        return false;
    }

    @Override
    public void onActivityDestroyed(Activity activity) {

    }

    /**
     * app是否处于前台
     */
    public boolean isFrontGround() {
        return appCount > 0;
    }
}
